<?php
namespace App\Intelihelp\Controller;

use Foxforms\Widget\Field\Radio;

use Foxforms\Widget\Field\Select;

use Foxforms\Widget\Form;

use Foxforms\Widget\Field\Input;

class tipoController extends \Foxframe\Controller
{
	public function index($params = array())
	{
		$this->redirect(BASE_PATH.'intelihelp/tipo/viewListar');
	}

	public function viewListar($params = array())
	{
		$lista = $this->em->getRepository('Entities\Intelihelp\Tipo')->listar();
		$this->view->assign('lista', $lista);
	}

	public function viewSalvar($params = array())
	{
		if (isset($params['id']) && !empty($params['id']))
			$tipo	= $this->em->find('\Entities\Intelihelp\Tipo', $params['id']);
		else
			$tipo	= new \Entities\Intelihelp\Tipo();

		$form	= new Form();
		$form->addValues($tipo, 'tipo');
		$form->addElement(Input::create('application')->setType('hidden')->setValue('intelihelp'));
		$form->addElement(Input::create('controller')->setType('hidden')->setValue('tipo'));
		$form->addElement(Input::create('method')->setType('hidden')->setValue('saveSalvar'));
		$form->addElement(Input::create('tipo[version]')->setType('hidden'));
		$form->addElement(Input::create('tipo[id_tipo]')->setType('hidden'));
		$form->addElement(
				Input::create('tipo[ds_tipo]')
				->setLabel('Tipo')
				->validate()
		);
		$form->addElement(
				Radio::create('tipo[fl_ativo]')
				->setLabel('Status')
				->setOption(array(1 => 'Ativo', 0 => 'Inativo'))
				->validate()
		);
		$form->addElement(Input::create('salvar')->setType('submit')->setValue('Salvar')->addClass('btn btn-primary'));
		$this->view->assign('form', $form);
		$this->returnAjax();
	}

	public function saveSalvar($params = array())
	{
		try
		{
			$service	= new \Foxframe\Service();
			$service->save($params['tipo'], '\Entities\Intelihelp\Tipo');
			$this->em->flush();

			$this->setFlash('Success', 'Sucesso', 'Tipo salvo com sucesso');
		}
		catch (\Exception $e)
		{
			$this->setFlash('Error', 'Erro ao salvar tipo', $e->getMessage());
		}
		$this->redirect(BASE_PATH.'intelihelp/tipo');
	}
}